package com.demo;


import java.util.*;

public class Application {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("1", "A");
        map.put("2", "A");
        map.put("3", "B");
        map.put("4", "A");
        map.put("5", "A");

        String value = "A";
        // 定义一个方法将hashmap和value传过去
        List<String> arrayList = formValueGetKey(map, value);

        if (!arrayList.isEmpty()) {
            System.out.println("Value为:" + value + "的key有:"+arrayList.size()+"几个"+"分别是:");
            for (int i = 0; i < arrayList.size(); i++) {
                System.out.println(arrayList.get(i));
            }
        }
    }

    private static List<String> formValueGetKey(Map<String, String> map, String value) {
        // 通过entrySet和iterator操作
        Set<Map.Entry<String, String>> entries = map.entrySet();
        List<String> arr = new ArrayList<>();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            if (entry.getValue().equals(value)) {
                arr.add(entry.getKey());
            }
        }
        return arr;
    }

}
